export const errorHandler = async (ctx, next) => {
  try {
    await next()
  } catch (error) {
    const peerId = ctx.request.query.peerid || 'unknown'
    console.error(`Error for peerId ${peerId}:`, error)
    ctx.status = error.status || 500
    ctx.body = {
      peerid: peerId,
      message: error.message || 'Internal Server Error',
      status: ctx.status,
    }
    if (process.env.NODE_ENV === 'development') {
      ctx.body.stack = error.stack
    }
  }
}
